package com.ytrsoft.ui;

import com.ytrsoft.base.JRoundedPane;
import com.ytrsoft.util.Message;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.Toolkit;
import java.util.List;

public class RightPane extends JPanel {

    private final JPanel contentPanel;

    public RightPane() {
        setLayout(new BorderLayout());
        contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));

        JScrollPane scrollPane = new JScrollPane(contentPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBorder(null);

        add(scrollPane, BorderLayout.CENTER);
    }

    public void setLyricist(List<String> lyricist) {
        contentPanel.removeAll();
        for (String lyrics : lyricist) {
            JRoundedPane lyricCard = new JRoundedPane();
            lyricCard.setLayout(new GridBagLayout());
            lyricCard.setBorder(new EmptyBorder(5, 5, 5, 5));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            gbc.insets = new Insets(5, 5, 5, 5);

            String[] group = lyrics.split("，");
            for (String line : group) {
                gbc.gridx = 0;
                gbc.gridy = GridBagConstraints.RELATIVE;
                JLabel lyricLabel = new JLabel(line);
                lyricLabel.setBorder(new EmptyBorder(5, 0, 5, 0));
                lyricCard.add(lyricLabel, gbc);

                gbc.gridx = 1;
                gbc.weightx = 0.0;
                JButton copyButton = new JButton("复制");
                copyButton.setPreferredSize(new Dimension(60, 26));
                copyButton.addActionListener(e -> copyToClipboard(line));
                lyricCard.add(copyButton, gbc);
            }

            contentPanel.add(lyricCard);
            contentPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        }

        contentPanel.revalidate();
        contentPanel.repaint();
    }

    private void copyToClipboard(String text) {
        StringSelection stringSelection = new StringSelection(text);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
        Message.info("复制成功");
    }
}
